/*
 * @Description: 腾讯地图方法封装
 * @Author: Caroline
 * @Date: 2024-01-29 01:03:18
 * @LastEditTime: 2024-02-21 18:01:38
 * @LastEditors: Caroline
 * @FilePath: \ronghe-web\src\hooks\useTencentMap.ts
 */
import axios from "axios";

// 经纬度，等位置信息
export interface LocationData {
  lat: number;
  lng: number;
  type?: string;
}
// 定位flag，标记定位返回结果的类型
export enum LocationFlag {
  LocationSuccss = 1, // gps定位，给精确定位
  LocationIpSuccss = 2, // ip定位，给ip定位
  LocationFailed = 3 // 定位失败，给默认定位
}
// 返回的定位结果
export type LocationResult = {
  location: LocationData;
  flag: LocationFlag;
};

export default () => {
  //   const TENCENT_MAP_KEY = "YP3BZ-UT6K3-CEQ3T-OUKDH-JFKIH-I5BR6";
  //   const TENCENT_MAP_KEY = "DNLBZ-CLK6W-NSZRZ-Y3XU7-ZGEAT-B7FHJ";

  // 腾讯地图-配置
  const tencentMapConfig = {
    // 我的应用key
    GEO_KEY: "PSVBZ-TS7KT-O5VX2-LUBX7-RAHY7-4MBNC",
    // 我的应用名称
    GEO_REFERER: "sodaweb",
    // 获取当前位置js
    GEO_LOCATION_JS: "https://mapapi.qq.com/web/mapComponents/geoLocation/v/geolocation.min.js",
    // 获取当前位置js的script 元素id
    GEO_LOCATION_JS_ID: "geo_Location"
  };

  // 引入地理位置js
  const importGeoLocationJs = () => {
    const geoScriptEl = document.getElementById(tencentMapConfig.GEO_LOCATION_JS_ID);
    if (geoScriptEl) {
      return geoScriptEl;
    }
    const bodyEl = document.querySelector("body");
    const scriptEl = document.createElement("script");
    scriptEl.setAttribute("type", "text/javascript");
    scriptEl.setAttribute("src", tencentMapConfig.GEO_LOCATION_JS);
    scriptEl.setAttribute("id", tencentMapConfig.GEO_LOCATION_JS_ID);
    scriptEl.setAttribute("allow", "geolocation");
    bodyEl?.appendChild(scriptEl);
    return scriptEl;
  };

  // 根据终端ip获取当前位置
  const getLocation = (): Promise<{ lat: number; lng: number }> => {
    return new Promise((resolve, reject) => {
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      const geolocation = new qq.maps.Geolocation(
        tencentMapConfig.GEO_KEY,
        tencentMapConfig.GEO_REFERER
      );
      geolocation.getLocation(
        (successRes: any) => {
          console.log("获取地址--success=", successRes);
          resolve({
            lat: successRes.lat,
            lng: successRes.lng
          });
        },
        (errRes: any) => {
          console.log("errRes=", errRes);
          reject(`获取地址异常：${JSON.stringify(errRes)}`);
        },
        { timeout: 60000 }
      );
    });

    // 获取ip位置
    // return await axios({
    //   baseURL: "",
    //   url: `/ws/location/v1/ip`,
    //   //   url: `/ws/place/v1/search`,
    //   params: {
    //     key: TENCENT_MAP_KEY
    //     // boundary: { lat: "", lng: "", radius: [500] }
    //   }
    // })
    //   .then((res: any) => {
    //     console.log("location-res--", res);
    //     const { location } = res.data?.result || {};
    //     return {
    //       lat: location.lat,
    //       lng: location.lng
    //     };
    //   })
    //   .catch((error) => {
    //     console.log("location-res-error-", error);
    //   });
  };
  return {
    tencentMapConfig,
    getLocation
  };
};
